package com.fk.adapter;

import java.util.List;

import com.fk.regimen.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FaqItemExpandAdapter extends BaseExpandableListAdapter {

	private Context mContext;
	private LayoutInflater mInflater = null;
	//设置组视图的图片
	int[] logos = new int[] {R.drawable.faqproblemicon,R.drawable.faqdoctor,R.drawable.faqadvice};
	//设置组视图的显示文字
	private String[] generalsTypes = new String[] {"形成原因？","何时就医？","生活建议"};
	//子视图显示文字
	private String[][] generals;
		
	
	public FaqItemExpandAdapter(Context context,String[][] mgenerals){
		mContext = context;
		generals = mgenerals;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}
	
	
	
	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return generalsTypes.length;
	}

	//获取当前父item下的子item的个数
	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return generals[groupPosition].length;
	}

	//获取当前父item的数据
	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return generalsTypes[groupPosition];
	}

	//得到子item需要关联的数据
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return generals[groupPosition][childPosition];
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	//得到子item的ID
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	//设置父item组件
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.exlayout_parent, null);
			
		}
		
		GroupViewHolder gHolder = new GroupViewHolder();
		gHolder.img_exgroup = (ImageView) convertView.findViewById(R.id.img_exgroup);
		gHolder.tv_exgroup = (TextView) convertView.findViewById(R.id.tv_exgroup);
		
		//设置父item的图标和标题
		gHolder.img_exgroup.setImageResource(logos[groupPosition]);
        gHolder.tv_exgroup.setText(getGroup(groupPosition).toString());		
		
		return convertView;
	}

	//设置子item的组件
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null){
			convertView = mInflater.inflate(R.layout.exlayout_child, null);			
		}
		ChildViewHolder cholder = new ChildViewHolder();
		cholder.tv_exchild = (TextView) convertView.findViewById(R.id.tv_exchild);
		
		//设置数据
		cholder.tv_exchild.setText(getChild(groupPosition, childPosition).toString());
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		
		//很重要，如果实现ChildView点击事件，必须返回true
		return false;
	}
	
	
	
	private class GroupViewHolder{
		ImageView img_exgroup;
		TextView tv_exgroup;
	}
	
	private class ChildViewHolder{
		TextView tv_exchild;
	}

}
